package com.nl.controller.web.service;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.nl.controller.common.annotation.SystemServiceLog;
import com.nl.controller.common.exception.SmsException;
import com.nl.controller.common.service.CommonService;
import com.nl.controller.web.bean.SmsMgrInfo;
import com.nl.controller.web.mapper.SmsBusiMapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
@Slf4j
public class SmsBusiService {

    @Autowired
    private SmsBusiMapper smsBusiMapper;

    @Autowired
    private CommonService commonService;

    @SystemServiceLog(description = "新增短信项目", value = false)
    public String addBusi(String name, String cityId,String extId,String countyId) {
        String id = smsBusiMapper.getBusiId();
        SmsMgrInfo smsMgrInfo;
        smsMgrInfo = new SmsMgrInfo();
        smsMgrInfo.setId(id);
        smsMgrInfo.setCityId(cityId);
        smsMgrInfo.setName(name);
        smsMgrInfo.setCityExtId(cityId+extId);
        smsMgrInfo.setCountyId(countyId);
        int flag = 0;
        try {
            flag = smsBusiMapper.addBusi(smsMgrInfo);
            smsBusiMapper.addSmsRealtimeControl(id);
            commonService.createSmsOperLog("2", "短信项目添加", "0", "短信项目维护");
        } catch (Exception e) {
            e.printStackTrace();
            commonService.createSmsOperLog("2", "短信项目添加", "1", "短信项目维护");
            log.error("短信项目添加出现异常：" + id);
            throw new SmsException("短信项目添加出现异常：" + id);
        }
        if (flag == 1) {
            smsBusiMapper.updateSmsExtInfo(extId,cityId);
            return id;
        } else {
            return "0";
        }
    }


    @SystemServiceLog(description = "根据条件查询短信项目的属性", value = false)
    public PageInfo<SmsMgrInfo> findBusi(int page, int limit, String state, String cityId, String mgrId,String mgrName,String countyId) {
        Map<String, Object> map = new HashMap<>();
        map.put("cityId", cityId);
        map.put("mgrId", mgrId);
        map.put("mgrName", mgrName);
        map.put("state", state);
        map.put("countyId", countyId);
        PageHelper.startPage(page, limit);

        List<SmsMgrInfo> list = new ArrayList<>();
        try {
            list = smsBusiMapper.findBusi(map);
            commonService.createSmsOperLog("4", "短信项目查询", "0", "短信项目维护");

        } catch (Exception e) {
            e.printStackTrace();
            commonService.createSmsOperLog("4", "短信项目查询", "1", "短信项目维护");
            log.error("短信项目查询异常" );
            throw new SmsException("短信项目查询" );
        }
        PageInfo<SmsMgrInfo> pageInfo = new PageInfo<>(list);
        return pageInfo;
    }


    @SystemServiceLog(description = "根据地市的id查询项目名", value = false)
    public List<SmsMgrInfo> findBusiByCityId(String cityId) {
        List<SmsMgrInfo> list = null;
        try {
            list = smsBusiMapper.findBusiByCityId(cityId);

        } catch (Exception e) {
            e.printStackTrace();
            log.error("根据地市的id查询项目名：" + cityId);
            throw new SmsException("根据地市的id查询项目名：" + cityId);
        }
        return list;
    }


    @SystemServiceLog(description = "修改项目的信息", value = false)
    public int modifyMgr(String id, String name, String cityId, String state) {
        int result = 0;
        Map<String, Object> map = new HashMap();
        map.put("id", id);
        map.put("name", name);
        map.put("cityId", cityId);
        map.put("state", state);
        try {
            result = smsBusiMapper.modifyMgr(map);
            commonService.createSmsOperLog("3", "短信项目修改:"+id, "0", "短信项目维护");
        } catch (Exception e) {
            commonService.createSmsOperLog("3", "短信项目修改:"+id, "1", "短信项目维护");
            e.printStackTrace();
            log.error("短信项目修改：" + id);
            throw new SmsException("短信项目修改：" + id);
        }
        return result;
    }


    @SystemServiceLog(description = "查询单个信息的项目",value = false)
    public SmsMgrInfo findByMgrId(String mgrId){
        SmsMgrInfo smsMgrInfo= null;
        try {
            smsMgrInfo = smsBusiMapper.findByMgrId(mgrId);
        } catch (Exception e) {
            e.printStackTrace();
            log.error("查询项目信息：" + mgrId);
            throw new SmsException("查询项目信息：" + mgrId);
        }
        return smsMgrInfo;
    }
}
